﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public delegate void EventCallBack(InfoPhoneCall phoneCall);

    public partial class phoneCallControl : BaseUserControl
    {

        private PhoneCallCtrl phoneCall = null;
        private DataTable dtPhoneCall = new DataTable();
        
        public phoneCallControl()
        {
            InitializeComponent();
            
        }

        public void initData()
        {
            
            phoneCall = PhoneCallCtrl.getPhoneCallCtrl(this);
            phoneCall.callBack = writePhone;

            dtPhoneCall = PhoneLog.GetPhoneLogDataSet();
            grdPhoneCall.DataSource = dtPhoneCall;
            if (phoneCall.IsStartCall)
            {
                btnCall.Text = "关闭来电显示";
            }
            else
            {
                btnCall.Text = "启用来电显示";
            }
            gvPhoneCall.ExpandAllGroups();
        }
      
        private void btnSetPhone_Click(object sender, EventArgs e)
        {
            frmSetComPort frmComPort = new frmSetComPort();
            if (frmComPort.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            phoneCall = PhoneCallCtrl.getPhoneCallCtrl(this);
            phoneCall.InitPort();

        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            if (btnCall.Text.Equals("启用来电显示"))
            {
                phoneCall.InitPort();
            }
            else
            {
                phoneCall.StopPort();
             
            }
            if (phoneCall.IsStartCall)
            {
                btnCall.Text = "关闭来电显示";
            }
            else
            {
                btnCall.Text = "启用来电显示";
            }
        }

        private void writePhone(InfoPhoneCall phoneCall)
        {
            gvPhoneCall.BeginUpdate();
            try
            {
                PhoneLog.SaveLog(phoneCall);
            }
            finally
            {
                gvPhoneCall.EndUpdate();
            }
        }

        private void buttonItem1_Click_1(object sender, EventArgs e)
        {
            //initData();
            phoneCall.testShowWindow();
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            if (gvPhoneCall.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的来电会员。");
                return;
            }
            DataSet dsCustomInfo = new DataSet();
            string error = string.Empty;
            DataRow drPhone = gvPhoneCall.GetDataRow(gvPhoneCall.FocusedRowHandle);
            if (Convert.ToInt16(drPhone[InfoPhoneCall.FieldName.CUSTOMERID]) == -1)
            {
                MsgBox.MsgInfo("没有该会员信息。");
                return;
            }
            if(!BussinessProxyManager.Instance.QueryCustomerID(Convert.ToInt16(drPhone[InfoPhoneCall.FieldName.CUSTOMERID]), out dsCustomInfo, out error)){
                MsgBox.MsgInfo("获取用户信息出错：" + error);
                return;
            }
            if (dsCustomInfo.Tables[0].Rows.Count == 0)
            {
                MsgBox.MsgInfo("没有找到该会员信息。");
                return;
            }
            frmCustomReservationMonth frm = new frmCustomReservationMonth();
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(dsCustomInfo.Tables[0].Rows[0]);
            frm.QueryReservation(DateTime.Now, customInfo);
            frm.ShowDialog();
        }

    }


}
